<?php 
//error_reporting(E_ALL);

## include required files
/*******************************/
require_once '../includefiles.php';
require_once '../model/users.php';
require_once '../model/config.php';
require_once '../model/background_images.php';
/*******************************/

## Create Objects
/*******************************/
$userObj = new Model_Users();
$configObj = new Model_Config();
$imageObj  = new Model_Background_Images();
/*******************************/

##fetching the logo
$logoArr = $configObj->getConfigValueById(1);
$smarty->assign('logoArr', $logoArr);





## Check if is admin already logged in
/*******************************/
if(isset($_SESSION["admin_id"]) != '' && trim($_SESSION["admin_id"]) != '')
{
	header("location:".SITE_URL."/admin/home.php");	
	exit;
}
/*******************************/

if(isset($_POST['login_btn'])) {
	## apply PHP validation for required fields whenever you submit the form in your project
	if(trim($_POST['username']) != '') {
		if(trim($_POST['password']) != '') {
			$userDetails = $userObj->getUserValueByDetailsUsernameAndPassword(trim($_POST['username']), trim($_POST['password']));
			if(count($userDetails) > 0) {
					if($userDetails['user_status'] == '1') {
						$_SESSION["admin_id"] = $userDetails['id'];
						$_SESSION["admin_username"] = $userDetails['username'];
						$_SESSION["admin_email"] = $userDetails['email'];
						$_SESSION["admin_firstname"] = $userDetails['first_name'];
						$_SESSION["admin_lastname"] = $userDetails['last_name'];
						$_SESSION["admin_type"] = $userDetails['user_type'];
						header("location:".SITE_URL."/admin/home.php?q=dashboard");
						exit;	
					} else { // Count else
						$_SESSION['msg'] = 'Your account is inactive. Please contact site admin';
					}
				} else {
					$_SESSION['msg'] = 'Your login details are incorrect';
				}
			} else { // password else
				$_SESSION['msg'] = 'Please enter your passowrd';
			}
		} else { // username else
			$_SESSION['msg'] = 'Please enter your username';
	}
} // main if closed

## Assign success or error msg to smarty variable and unset session variable
if(trim($_SESSION['msg']) != '') {
	$smarty->assign('msg', $_SESSION['msg']);
	unset($_SESSION['msg']);
}

## Fetch footer logo and text setting
# Footer text
$footer_text = $configObj->getConfigValueById(2);
$smarty->assign('footer_text', $footer_text['config_value']);

# Footer logo
$footer_logo = $configObj->getConfigValueById(3);
$smarty->assign('footer_logo', $footer_logo['config_value']);

## Unset all the objects created which are on this page
unset($userObj);
unset($configObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'index.tpl');
unset($smarty);
?>